package com.sys.server.modules.controller;

import com.sys.server.common.annotation.Authentication;
import com.sys.server.common.annotation.Log;
import com.sys.server.common.model.R;
import com.sys.server.modules.controller.dto.ParticipantDto;
import com.sys.server.modules.controller.dto.PathDto;
import com.sys.server.modules.controller.dto.ProjectCommentDto;
import com.sys.server.modules.controller.dto.ProjectDto;
import com.sys.server.modules.service.FileService;
import com.sys.server.modules.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@Authentication
@Api(tags = "学生作品管理模块")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private FileService fileService;

    @RequestMapping(value = "/addProject", method = RequestMethod.POST)
    @ApiOperation(value = "学生作品管理模块--添加学生作品")
    public R addProject(ProjectDto projectDto) {
        return projectService.addProject(projectDto);
    }

    @RequestMapping(value = "/getProList", method = RequestMethod.GET)
    @ApiOperation(value = "学生作品管理模块--分页获取项目列表")
    public R getProList(@RequestParam Long pagenum,
                        @RequestParam Long pagesize) {
//        return projectService.getProList(pagenum, pagesize);
        return projectService.getProListPersonal(pagenum, pagesize);
    }

    @RequestMapping(value = "/getProInfoById", method = RequestMethod.GET)
    @ApiOperation(value = "学生作品管理模块--根据id查询学生作品信息")
    public R getProInfoById(@RequestParam Long id) {
        return projectService.getProInfoById(id);
    }

    @RequestMapping(value = "/updateProInfo", method = RequestMethod.POST)
    @ApiOperation(value = "学生作品管理模块--更新作品信息")
    @Log(module = "项目管理", action = "更新项目信息", type = "修改")
    public R updateProInfo(@RequestBody ProjectDto projectDto) {
        return projectService.updateProInfo(projectDto);
    }

    @RequestMapping(value = "/addParticipant", method = RequestMethod.POST)
    @ApiOperation(value = "学生作品管理模块--添加项目成员")
    @Log(module = "项目管理", action = "添加项目成员", type = "新增")
    public R addParticipant(@RequestBody ParticipantDto participantDto) {
        return projectService.addParticipant(participantDto);
    }

    @RequestMapping(value = "/getParticipate", method = RequestMethod.GET)
    @ApiOperation(value = "学生作品管理模块--获取项目成员")
    public R getProParticipate(@RequestParam Long id) {
        return projectService.getProParticipant(id);
    }

    @RequestMapping(value = "/deleteParticipate", method = RequestMethod.POST)
    @ApiOperation(value = "学生作品管理模块--删除项目成员")
    @Log(module = "项目管理", action = "删除项目成员", type = "删除")
    public R deleteProParticipate(@RequestBody ParticipantDto participantDto) {
        return projectService.deleteProParticipant(participantDto);
    }

    @RequestMapping(value = "/updateProRole", method = RequestMethod.POST)
    @ApiOperation(value = "学生作品管理模块--修改个人信息（学生）")
    public R updateProRole(@RequestBody ParticipantDto participantDto) {
        return projectService.updateProRole(participantDto);
    }

    @RequestMapping(value = "/addProgress", method = RequestMethod.POST)
    @ApiOperation(value = "学生作品管理模块--添加项目动态（不上传附件）")
    @Log(module = "项目管理", action = "添加项目动态", type = "新增")
    public R addProgress(@RequestBody ProjectDto projectDto) {
        return projectService.addProgress(projectDto);
    }

    @RequestMapping(value = "/addProgressAttach", method = RequestMethod.POST)
    @ApiOperation(value = "学生作品管理模块--添加项目动态（上传附件）")
    public R addProgressAttach(ProjectDto projectDto) {
        return projectService.addProgress(projectDto);
    }

    @RequestMapping(value = "/updateComment", method = RequestMethod.POST)
    @ApiOperation(value = "学生作品管理模块--修改评语（老师）")
    public R updateComment(@RequestBody ProjectCommentDto projectCommentDto) {
        return projectService.updateProComment(projectCommentDto);
    }

    @RequestMapping(value = "/getDetail", method = RequestMethod.GET)
    @ApiOperation(value = "学生作品管理模块--获取项目详细信息")
    public R getDetail(@RequestParam Long projid) {
        return projectService.getDetail(projid);
    }

    @RequestMapping(value = "/deletePro", method = RequestMethod.POST)
    @ApiOperation(value = "学生作品管理模块--删除项目")
    @Log(module = "项目管理", action = "删除项目", type = "删除")
    public R deletePro(@RequestBody ProjectDto projectDto) {
        return projectService.deletePro(projectDto);
    }

    @RequestMapping(value = "/downloadProAttach", method = RequestMethod.POST)
    @ApiOperation(value = "学生作品管理模块--下载项目附件")
    public void downloadAttach(HttpServletResponse response, @RequestBody ProjectDto projectDto) throws IOException {
        String path = projectService.getAttachPath(projectDto.getProjid());
        fileService.fileDownload(response, path);
    }

    @RequestMapping(value = "/downloadProgressAttach", method = RequestMethod.POST)
    @ApiOperation(value = "学生作品管理模块--项目动态附件下载")
    public void downloadProgressFile(HttpServletResponse response, @RequestBody PathDto pathDto) throws IOException {
        String path = pathDto.getPath();
        fileService.fileDownload(response, path);
    }

}
